package com.chanjiedata.changbs.module.main;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.dialog.RecycleViewDialog;
import com.chanjiedata.changbs.dialog.WheelDialog;
import com.chanjiedata.changbs.help.UserHelper;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.proto.PBPublicOuterClass;
import com.chanjiedata.changbs.utlis.DialogUtils;
import com.chanjiedata.changbs.utlis.GsonUtils;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kale.ui.view.dialog.EasyDialog;

/**
 * /**
 * * 文件名 : AllocaActivity
 * * 创建者 : MangoWang
 * * 创建日期 : 2019/11/26 15:19
 * * 微信 : MangoWaWang
 * * 邮箱 : 763482205@qq.com
 * * 描述 : TODO
 */

public class AllocaActivity extends BaseActivity {


    @BindView(R.id.tv_select_produce)
    TextView tvSelectProduce;
    @BindView(R.id.tv_select_sn_number)
    TextView tvSelectSnNumber;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.iv_line_hide)
    ImageView ivLineHide;
    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private String sn_response;
    private String sn_all_code;
    private RecycleViewDialog build;
    private RecycleViewDialog build2;
    private String goods_id;
    private String MyerroMessage;
    private boolean reset = false;  // 用于刷新不同机型的sn号数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alloca);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        initListener();
        tvTitle.setText("发起调拨");
    }

    private void initListener() {
        backTv.setOnClickListener(view -> finish());
        tvSelectProduce.setOnClickListener(view -> {
            WheelDialog.Builder builder = new WheelDialog.Builder(mContext);
            builder.setCancelable(true);
            builder.setIsBottomDialog(true);
            builder.setData(getIntent().getStringExtra("data"));
            builder.setOnDialogClickListener((v, goods_name, goods_id) -> {
                tvSelectProduce.setText(goods_name);
                this.goods_id = goods_id;
                LoadSnData(goods_id);
            });
            EasyDialog build = builder.build();
            build.show(getSupportFragmentManager());
        });
        tvSelectSnNumber.setOnClickListener(view -> {
            boolean empty = TextUtils.isEmpty(sn_response);
            if (empty) {
                if (TextUtils.isEmpty(MyerroMessage)) {
                    showToast("请先选择调拨机器类型");
                } else {
                    showToast(MyerroMessage);
                }
            } else {
                showSelectSnDialog();
            }

        });
        btnSubmit.setOnClickListener(view -> submit());
        tvName.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, AllocaPartnerActivity.class);
            intent.putExtra("title", "选择调拨的伙伴");
            intent.putExtra("type", "2");
            startActivityForResult(intent, 100);
        });

    }

    private void showSelectSnDialog() {
        DialogUtils.showBottomMenuDialog(mContext, "选择方式", "单个", "区间", new DialogUtils.BottomMenuDialogImpl() {
            @Override
            public void onMenu1() {
                if (build != null && !reset) {
                    build.show(getSupportFragmentManager());
                } else {
                    RecycleViewDialog.Builder builder = new RecycleViewDialog.Builder(mContext);
                    builder.setData(sn_response).setType("only");
                    builder.setOnDialogClickListener((v, sn_code, sn_count, sn_up_code) -> {
                        tvSelectSnNumber.setText(sn_code);
                        tvNumber.setText(sn_count);
                        sn_all_code = sn_up_code;
                    });
                    build = builder.build();
                    build.show(getSupportFragmentManager());
                    reset = false;
                }
            }

            @Override
            public void onMenu2() {
                if (build2 != null && !reset) {
                    build2.show(getSupportFragmentManager());
                } else {
                    RecycleViewDialog.Builder builder = new RecycleViewDialog.Builder(mContext);
                    builder.setData(sn_response).setType("que");
                    builder.setOnDialogClickListener((v, sn_code, sn_count, sn_up_code) -> {
                        tvSelectSnNumber.setText(sn_code);
                        tvNumber.setText(sn_count);
                        sn_all_code = sn_up_code;
                    });
                    build2 = builder.build();
                    build2.show(getSupportFragmentManager());
                    reset = false;
                }
            }
        });



    }

    private void submit() {

        if (TextUtils.isEmpty(goods_id)) {
            showToast("请先选择调拨产品");
            return;
        }

        if (TextUtils.isEmpty(sn_all_code)) {
            showToast("请先选择SN号");
            return;
        }

        String number = tvNumber.getText().toString();
        String name = tvName.getText().toString();
        String member_mobile = tvPhone.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请先输入伙伴姓名");
            return;
        }
        if (TextUtils.isEmpty(member_mobile)) {
            showToast("请先输入伙伴手机号");
            return;
        }
        Mystock.initiateATransferNew.Builder builder = Mystock.initiateATransferNew.newBuilder();
        builder.setGoodsId(goods_id).setSnCode(sn_all_code).setMemberName(name)
                .setMemberMobile(member_mobile).setNum(number);
        NetworkManager.INSTANCE.post(Apis.initiateATransfer,builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
                if (errorCode == 200) {
                    finish();
                }
            }
        });
    }


    private void LoadSnData(String goods_id) {
        Mystock.showProductSNCode.Builder builder = Mystock.showProductSNCode.newBuilder();
        builder.setGoodsId(goods_id);
        NetworkManager.INSTANCE.post(Apis.showProductSNCode, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                List<Mystock.showProductSNCode.List> lists = Mystock.showProductSNCode.parseFrom(bytes).getListList();
                sn_response = GsonUtils.serializedToJson(lists);
                reset = true;
                tvSelectSnNumber.setText("");
                tvNumber.setText("");
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                sn_response = "";
                MyerroMessage = errorMessage;
                showToast(errorMessage);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            tvName.setText(data.getStringExtra("name"));
            tvPhone.setText(data.getStringExtra("phone"));
            if (tvPhone.getText().toString().equals("1")) {
                llPhone.setVisibility(View.GONE);
                ivLineHide.setVisibility(View.GONE);
            } else {
                llPhone.setVisibility(View.VISIBLE);
                ivLineHide.setVisibility(View.VISIBLE);
            }
        }
    }
}
